package com.xpgk.mvc.user;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.xpgk.dto.ZtreeNode;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.dept.Department;
import com.xpgk.mvc.deptStation.DeptStation;

/**
 * 用户管理
 */
public class UserController extends BaseController {

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(UserController.class);

	private UserService userService;

	/**
	 * 默认列表
	 */
	public void index() {
		render("/user/usertree.html");
	}

	public void userList() {
		paging(splitPage, User.sqlId_splitPageSelect, User.sqlId_splitPageFrom);
		render("/user/userList.html");
	}

	/**
	 * 保存新增用户
	 */
	@Before(UserValidator.class)
	public void save() {
		String ids = getAttr("ids");

		String password = getPara("password");
		User user = getModel(User.class);
		UserInfo userInfo = getModel(UserInfo.class);

		userService.save(ids, user, password, userInfo);

		forwardAction("/user/backOff");
	}

	/**
	 * 准备更新
	 */
	public void edit() {
		User user = User.dao.findById(getPara());
		setAttr("user", user);
		setAttr("userInfo", UserInfo.dao.findById(user.getPKValue()));
		setAttr("station", DeptStation.dao.findById(user.get(User.column_stationids)));
		setAttr("dept", Department.dao.findById(user.get(User.column_departmentids)));
		render("/user/update.html");
	}

	/**
	 * 更新
	 */
	@Before(UserValidator.class)
	public void update() {
		String password = getPara("password");
		User user = getModel(User.class);
		UserInfo userInfo = getModel(UserInfo.class);

		userService.update(user, password, userInfo);
		forwardAction("/user/backOff");
	}

	/**
	 * 查看
	 */
	public void view() {
		User user = User.dao.findById(getPara());
		setAttr("user", user);
		setAttr("userInfo", user.getUserInfo());
		setAttr("station", DeptStation.dao.findById(user.get(User.column_stationids)));
		setAttr("dept", Department.dao.findById(user.get(User.column_departmentids)));
		render("/user/view.html");
	}

	/**
	 * 删除
	 */
	public void delete() {
		userService.delete(getPara() == null ? ids : getPara());
		forwardAction("/user/backOff");
	}

	/**
	 * 用户树ztree节点数据
	 */
	public void treeData() {
		List<ZtreeNode> list = userService.childNodeData(getCxt(), getPara("deptIds"));
		renderJson(list);
	}

	/**
	 * 验证旧密码是否正确
	 */
	public void valiPassWord() {
		String passWord = getPara("passWord");
		boolean bool = userService.valiPassWord(ids, passWord);
		renderText(String.valueOf(bool));
	}

	/**
	 * 密码变更
	 */
	public void passChange() {

		String ids=getCUserIds();
		String passOld = getPara("oldPassword");
		String passNew = getPara("newPassword");
		boolean  bool = userService.passChange(ids, passOld, passNew);

		renderText(String.valueOf(bool));
	}
	
	/**
	 * 登录时密码变更
	 */
	public void passLoginChange() {

		String username=getPara("username");
		String passOld = getPara("oldPassword");
		String passNew = getPara("newPassword");
		  if(passNew.matches("^[a-zA-z](.*)") && passNew.matches("(.*)[-`=\\\\\\[\\];',./~!@#$%^&*()_+|{}:\"<>?]+(.*)") && passNew.matches("(.*)\\d+(.*)")){  
			  boolean  bool = userService.passChange(ids, passOld, passNew,username);
			  if(bool){
				  renderText(String.valueOf(bool));
			  }else{
				  renderText("旧密码错误");
			  }
			  
		  }else{
			  renderText("必须以字母开头,必须包含特殊字符 ,必须包含数字");
		  }
		

		
	}

}
